<?php

namespace App\Repositories;

use App\Models\TemplateAccept;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * 消息模板
 *
 * Class TemplateAcceptRepository
 * @package App\Repositories
 */
class MessageTemplateRepository
{

    /**
     * 添加一个模板
     *
     * @param string $WxTemplateId
     * @param string $name
     * @param string $content
     *
     * @return int
     */
    public function add(string $WxTemplateId, int $uid): int
    {
        $TemplateAccept = new TemplateAccept();
        $TemplateAccept->template_id = $WxTemplateId;
        $TemplateAccept->uid = $uid;
        $TemplateAccept->save();
        return $TemplateAccept->id;
    }

    /**
     * 获取所有模板
     *
     * @return TemplateAccept[]|Collection
     */
    public function list($type)
    {
        if (is_array($type)) {
            return TemplateAccept::whereIn('type', $type)->get();
        } else {
            return TemplateAccept::where('type', $type)->get();
        }
    }

    /**
     * 删除（软删除）一个模板，如果不存在不处理
     *
     * @param $id
     */
    public function del($id)
    {
        $TemplateAccept = TemplateAccept::find($id);
        if ($TemplateAccept) {
            $TemplateAccept->delete();
        }
    }

    /**
     * @param $id
     *
     * @return TemplateAccept
     */
    public function getOne($id)
    {
        return TemplateAccept::find($id);
    }

    /**
     * @param $WxTemplateId
     *
     * @return TemplateAccept
     */
    public function fromWxTemplate($WxTemplateId, $uio)
    {
        return TemplateAccept::where(['template_id' => $WxTemplateId, "uid" => $uio])->first();
    }

    public function addAll($tmpIds = [], $uid = null)
    {
        if (!is_array($tmpIds) || !$uid)
            return false;

        foreach ($tmpIds as $k => $v) {
            if (TemplateAccept::query()->where(['uid' => $uid, 'template_id' => $v])->count("id"))
                continue;

            $this->add($v, $uid);
        }

        return true;
    }

    public function saveOrUpdate($accept = [], $uid = null)
    {
        foreach ($accept as $k => $v) {
            $res = TemplateAccept::query()->where(['uid' => $uid, 'template_id' => $k])->first();

            if ($v != 'accept') {
                if ($res) {
                    TemplateAccept::query()->where("id", $res['id'])->delete();
                }

                continue;
            }

            if ($v === 'accept' && $res) {
                continue;
            }

            $this->add($k, $uid);
        }

        return true;
    }

    /**
     * 获取已接收模板
     * @param null $uid
     * @param null $type
     * @return \App\Models\BaseModel|false|void
     */
    public function getAccept($uid = null, $type = null)
    {
        if (!intval($uid)) return;
        $template_id = TemplateAccept::getTmpId($type);
        if (empty($template_id))
            return false;

        return TemplateAccept::query()->where(['uid' => $uid, 'template_id' => $template_id])->first();
    }
}
